;
(function () {
  new Vue({
    el: '#assistAccounting',
    data: {
      accountingArr: [],
      accountingSelectId: '',
      ops: {
        bar: {
          background: '#c1c1c1'
        }
      },

      dialogFlag: false,
      dictList: [],
      tableList: [],
      assistName: '',
      businessType: [],
      businessTypeValue: '',
      memo: '',

      editId: '',



      currentPage: 1,
      totalPage: 0,
      totalCount: 0,
      limit: ['10'], // 条数
      pageLimits: [{
        name: '10',
        code: '10'
      }, {
        name: '15',
        code: '15'
      }, {
        name: '20',
        code: '20'
      }],


      entryId: '',
      keyword: '',
      detailList: []

    },
    methods: {
      //获取账套列表
      accountingsDt() {
        hlhttp.get({
          url: 'acct/accountings',
          success: (data) => {
            if (!data.length) return;
            this.accountingArr = data;
            this.accountingSelectId = sessionStorage.getItem("accountId");
            this.getList()
          }
        })
      },
      //选择账套
      selectAccounting(item) {
        this.accountingSelectId = item.id;
        sessionStorage.setItem("accountId", this.accountingSelectId);
        this.entryId = ''
        this.getList()
      },
      // 获取字典
      getDict() {
        hlhttp.get({
          url: 'configure/dict',
          data: {
            'module_code': 'CIWU',
            'config_name': 'CW_ASSIST_BUSINESS_TYPE'
          },
          success: res => {
            res.map(item => {
              item.name = item.dictName
              item.code = item.dictCode
            })
            this.dictList = res
          }
        })
      },
      // 获取数据
      getList() {
        let data = {
          cash_flag: 'N',
          accounting_id: this.accountingSelectId + ''
        }
        hlhttp.get({
          url: 'acct/acct_assist/manage_list',
          data: data,
          success: res => {
            this.tableList = res
          }
        })
      },

      // 新增
      addFun() {
        this.dialogFlag = true
        this.editId = ''
        this.assistName = ''
        this.businessType = []
        this.memo = ''
      },
      // 编辑
      editItem(item) {
        this.dialogFlag = true
        this.editId = item.id
        this.assistName = item.assistName
        this.businessType = [item.businessType]
        this.businessTypeValue = item.businessTypeValue
        this.memo = item.memo
      },
      // 删除
      delItem(id) {
        var that = this
        hllayer.confirm('此操作不可逆转，确认删除吗？', function () {
          hlhttp.delete({
            url: 'acct/acct_assist?id=' + id,
            success: function () {
              hl.toast('删除成功')
              that.getList();
            }
          })
        })
      },
      // 校验
      submitCheck() {
        if (!this.assistName) {
          hl.toast('请输入核算方法名称');
          return false
        }
        if (!this.businessType.length) {
          hl.toast('请选择业务对象');
          return false
        }
        return true
      },
      // 确定添加
      submitFun() {
        if (!this.submitCheck()) return
        let data = {
          accounting_id: this.accountingSelectId + '',
          assist_name: this.assistName + '',
          business_type: this.businessType + '',
          memo: this.memo || '#',
          cash_flag: 'N'
        }
        if (this.editId) {
          data.id = this.editId
          delete data.cash_flag
          delete data.accounting_id
          hlhttp.put({
            url: 'acct/acct_assist',
            data: data,
            success: res => {
              hl.toast('编辑成功');
              this.dialogFlag = false
              this.getList()
            }
          })
        } else {
          hlhttp.post({
            url: 'acct/acct_assist',
            data: data,
            success: res => {
              hl.toast('新增成功');
              this.dialogFlag = false
              this.getList()
            }
          })
        }
      },
      // 录入明细
      entryFun(id) {
        this.entryId = id
        this.keyword = ''
        this.getDetailList()
      },
      // 分页
      updateLimit(arr) {
        this.limit = [arr[0].code];
        this.currentPage = 1;
        this.getDetailList()
      },
      // 获取明细列表
      getDetailList(val) {
        if(val == 'search'){
          this.currentPage = 1
        }
        hlhttp.get({
          url: 'acct/acct_assist/detail/list',
          data: {
            keyword: this.keyword || '#',
            assist_id: this.entryId,
            limit: this.limit + '',
            page: this.currentPage + '',
          },
          success: res => {
            this.totalCount = res.totalCount
            this.totalPage = res.totalPage
            res.datas.map(item => {
              item.isEdit = false
              item.assistCodeCopy = ''
              item.assistNameCopy = ''
            })
            this.detailList = res.datas
          }
        })
      },
      // 编辑（明细）
      editDetailItem(index) {
        this.detailList[index].isEdit = true
        this.detailList[index].assistCodeCopy = JSON.parse(JSON.stringify(this.detailList[index].assistCode))
        this.detailList[index].assistNameCopy = JSON.parse(JSON.stringify(this.detailList[index].assistName))
      },
      saveDetailItem(item) {
        if (!item.assistCodeCopy) {
          hl.toast('请输入核算代码')
          return
        }
        if (!item.assistNameCopy) {
          hl.toast('请输入核算名称')
          return
        }
        let data = {
          id: item.id,
          assist_name: item.assistNameCopy,
          assist_code: item.assistCodeCopy
        }
        hlhttp.put({
          url: 'acct/acct_assist/detail_info',
          data: data,
          success: res => {
            hl.toast('编辑成功');
            this.getDetailList()
          }
        })
      },
      // 删除（明细）
      delDetailItem(id) {
        var that = this
        hllayer.confirm('此操作不可逆转，确认删除吗？', function () {
          hlhttp.delete({
            url: 'acct/acct_assist/detail_info?id=' + id,
            success: function () {
              hl.toast('删除成功')
              that.getDetailList();
            }
          })
        })
      },
    },
    mounted() {
      this.accountingsDt()
      this.getDict()
    }
  })
}())